package com.lht.cozyCloud.bee.receiver;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.lht.cozyCloud.bee.service.MsmService;
import com.lht.cozyCloud.bee.vo.MsmVo;
import com.lht.cozyCloud.rabbitmq.constants.MqConst;
import com.rabbitmq.client.Channel;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * @author lht
 * @version 1.0
 * @description: 手机验证码队列消费者
 * @date 2025/1/7 10:38
 */
@Component
public class MsmReceiver {

    @Autowired
    private MsmService msmService;


    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MqConst.QUEUE_MSM_PHONE, durable = "true"),
            exchange = @Exchange(value = MqConst.EXCHANGE_DIRECT_MSM),
            key = {MqConst.ROUTING_MSM_PHONE}
    ))
    public void send(MsmVo msmVo, Channel channel, Message message) {
        try {
            String json = new String(message.getBody(), StandardCharsets.UTF_8);
            System.out.println("Received message: " + json);
            // 处理消息
        } catch (Exception e) {
            e.printStackTrace();
            // 处理异常
        }
        msmVo.setTemplateCode("");
        msmService.send(msmVo);
    }

}
